package com.qf.hospital.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.hospital.enums.HospitalExceptionEnums;
import com.qf.hospital.exception.HospitalException;
import com.qf.hospital.vo.R;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ASUS
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Date 2021/10/15 14:29
 */
@RestController
public class LoginController {

    private Logger log = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping("/sel/doLogin")
    public R doLogin(String loginname, String pwd){
        if(StringUtils.isEmpty(loginname) || StringUtils.isEmpty(pwd)){
            log.info("【登录功能】 请求参数异常，用户名和密码均为必填项!");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"用户名和密码均为必填项!");
        }
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(loginname,pwd));
        return R.ok("登陆成功");
    }

    @RequestMapping("/sel/logout")
    public R logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return R.ok("退出登录成功");
    }

}
